package fr.demos.cinepantin.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.UserTransaction;

import fr.demos.cinepantin.Admin;
import fr.demos.cinepantin.AdressePostal;
import fr.demos.cinepantin.Article;
import fr.demos.cinepantin.Client;
import fr.demos.cinepantin.dao.DaoClient;
import fr.demos.cinepantin.dao.interfac.ClientDao;
import fr.demos.cinepantin.enumerate.Categorie;
import fr.demos.cinepantin.exception.DaoExceptions;

/**
 * 
 * @author Demos Stagiaires CQP-DNT
 * 
 * Dao Regroupant tous les actions en relation de client
 *
 */

@DaoClient
public class ClientDaoImpl implements ClientDao{
	// Grace a l'injection l'objet entityManager est instancie
		@PersistenceContext(unitName = "cinemarc")
		private EntityManager em; // gestion des entity qui sont dans un contexte
		@Resource
		private UserTransaction ut;

		public ClientDaoImpl() {

		}
		
		@Override
		public void enregistrerClient(Client cli) throws DaoExceptions {
			try {
				ut.begin();
			} catch (Exception e) {
				e.printStackTrace();
			}

			em.persist(cli);

			try {
				ut.commit();
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoExceptions(e.getMessage(), e);
			}			
		}	

	@Override
	public boolean verifLoginClient(String email, String password)
			throws DaoExceptions {
		String query = "SELECT c FROM Client c WHERE ";
			query+=	" c.email = '" + email + "' AND c.password = '" + password + "'" ;
		TypedQuery<Client> q = em.createQuery(query, Client.class);
		if(q.getResultList().isEmpty()){
			return false;
		}
		return true ;		
	}

	@Override
	public Client getClientByEmail(String email) throws DaoExceptions {
		String query = "SELECT c FROM Client c WHERE";
		query+=	" c.email = '"+ email + "'";
		TypedQuery<Client> q = em.createQuery(query, Client.class);

		return q.getSingleResult();

	}

	@Override
	public Client getClientById(int idClient) throws DaoExceptions {
		Client client = null;
		client = em.find(Client.class, idClient);

		return client;
	}

	@Override
	public List<AdressePostal> getAdresseByClient(int idClient) throws DaoExceptions {
		// AdressePostal adressepostale = null;
		String query = "select a from AdressePostal a where c.idClient=" +idClient;
		TypedQuery<AdressePostal> q = em.createQuery(query, AdressePostal.class);
		List<AdressePostal> resAdressepostale = q.getResultList();
		return resAdressepostale;

	}
	

	@Override
	public AdressePostal getAdressePostaleById(int idAdressePostale) throws DaoExceptions {
		AdressePostal adressepostale = null;
		adressepostale = em.find(AdressePostal.class, idAdressePostale);

		return adressepostale;	
		
	}

	@Override
	public void enregistrer(Client c,AdressePostal a) throws DaoExceptions {
		
		a.setAdressePostal(a);
		
		try {
			ut.begin();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		em.persist(c);
		
		try {
			ut.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// @Override
//	public Client getClientByNom(String nom) throws DaoExceptions {
//		String query = "SELECT c FROM Client c WHERE";
//		query+=	" c.nom = '"+ nom + "'";
//		TypedQuery<Client> q = em.createQuery(query, Client.class);
//
//		return q.getSingleResult();
//}


}